package com.car.slave.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.car.slave.im.model.user.IMTextMessage;
import com.car.slave.im.model.user.IMUserMessage;
import com.imchezhil.R;

public class ChatAdapter extends BaseAdapter {
	
	private Context context;
	
	private List<IMUserMessage> data;
	
	public ChatAdapter(Context context, List<IMUserMessage> data) {
		super();
		this.context = context;
		this.data = data;
	}
	
	public List<IMUserMessage> getData() {
		return data;
	}

	public void addMessage(IMUserMessage msg) {
		if (data == null) {
			data = new ArrayList<IMUserMessage>();
		}
		data.add(msg);
	}

	@Override
	public int getCount() {
		return data == null ? 0 : data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getItemViewType(int position) {
		return data.get(position).direction;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		IMTextMessage msg = (IMTextMessage) data.get(position);

		if (getItemViewType(position) == 0) {
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(R.layout.item_chat_sender, null);
			} 
			TextView sendState = (TextView) convertView.findViewById(R.id.sendState);
			sendState.setText(getSendStatus(msg));
		} else {
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(R.layout.item_chat_receiver, null);
			}
		}
		TextView mesaage = (TextView) convertView.findViewById(R.id.message);
		mesaage.setText(msg.content);
		
		
		return convertView;
	}
	
	public String getSendStatus(IMTextMessage message) {
		if (message.sentStatus == 0) {
			return "发送中";
		} else if (message.sentStatus == 1) {
			return "发送失败";
		}
		return "发送成功";
	}
}
